package com.wxg.cmd;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Ignore;
import org.junit.Test;

/**
 * [利用Java.lang.Process和ProcessBuilder创建本地应用程序进程](http://www.cnblogs.com/ChrisWang/archive/2009/12/02/use-java-lang-process-and-processbuilder-to-create-native-application-process.html)
 * 
 * @author wxg
 *
 */
public class JavaCmdSimulatorDemo {

	
	@Test
	public void test006() throws IOException{
		String dir = "C:/wxg2/firstblog";
		String[] commands = null;
		commands = "cmd /c hexo server -p 5000".split(" ");
		runtimeExec(commands, dir);
	}
	
	
	/**
C:\wxg6\mytoolkit\mytoolkit
	 * @throws IOException
	 */
	@Test
	@Ignore
	public void test005() throws IOException{
		String dir = System.getProperty("user.dir");
		String[] commands = null;
		commands = "cmd /c echo %cd%".split(" ");
		runtimeExec(commands, dir);
	}
	
	/**
java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b25)
Java HotSpot(TM) Client VM (build 25.40-b25, mixed mode)
	 * @throws IOException
	 */
	@Test
	@Ignore
	public void test004() throws IOException{
		String dir = System.getProperty("user.dir");
		String[] commands = null;
		commands = "cmd /c java -version".split(" ");
		runtimeExec(commands, dir);
	}
	
	private void runtimeExec(List<String> commands, String dir) throws IOException{
		String[] arr = new String[]{};
		runtimeExec(commands.toArray(arr), dir);
	}
	
	private void runtimeExec(String[] commands, String dir) throws IOException{
		String[] arr = new String[]{};
		List<String> env = new ArrayList<String>();
		env.add("JAVA_HOME=C:/Java/jdk1.8.0_40");
		//System.getenv("Path"): Path,path都可以。
		env.add("PATH=" + System.getenv("Path"));
		runtimeExec(commands, env.toArray(arr), new File(dir));
	}
	
	private void runtimeExec(List<String> commands, List<String> env, File dir) throws IOException{
		String[] arr = new String[]{};
		runtimeExec(commands.toArray(arr), env.toArray(arr), dir);
	}
	
	private void runtimeExec(String[] commands, String[] env, File dir) throws IOException{
		Runtime currentRuntime = Runtime.getRuntime();
		Process process = currentRuntime.exec(commands, env, dir);
		InputStream in = process.getInputStream();
		InputStream errin = process.getErrorStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		BufferedReader errbr = new BufferedReader(new InputStreamReader(errin));
		try{
			/**
			 * 把`line`,`errline`分开打印似乎不合理！
			 */
			String line = null, errline = null;
			while((line=br.readLine()) != null){
				System.out.println( line );
			}
			while((errline=errbr.readLine()) != null){
				System.out.println( errline );
			}
		}finally{
			br.close();
			errbr.close();
		}
	}
	
	@Test
	@Ignore
	public void test003() throws IOException{
		fetchSysEnv();
		System.out.println("=====================");
		fetchSysProp();
//		Runtime currentRuntime = Runtime.getRuntime();
//		String command = null;
//		command = "dir";
//		Process process = currentRuntime.exec(command);
//		InputStream in = process.getInputStream();
//		BufferedReader br = new BufferedReader(new InputStreamReader(in));
//		String line = null;
//		while( (line=br.readLine()) != null ){
//			System.out.println( line );
//		}
//		br.close();
	}
	
	
	/**
	 * [深入研究java.lang.Process类](http://lavasoft.blog.51cto.com/62575/15599/)
	 * 
%cd%
	 * @throws IOException
	 */
	@Test
	@Ignore
	public void test002() throws IOException{
		Runtime currentRuntime = Runtime.getRuntime();
		String command = null;
		command = "echo %cd%";
		Process process = currentRuntime.exec(command);
		InputStream in = process.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String line = null;
		while( (line=br.readLine()) != null ){
			System.out.println( line );
		}
		br.close();
	}
	
	/**
	 * 
number of processor is : 8
JVM free memory size is : 12737456
maxMemory : 259522560
totalMemory : 16252928
	 */
	@Test
	@Ignore
	public void test001(){
		Runtime currentRuntime = Runtime.getRuntime();
		int numProcessor = currentRuntime.availableProcessors();
		long freeMemory = currentRuntime.freeMemory();
		long maxMemory = currentRuntime.maxMemory();
		long totalMemory = currentRuntime.totalMemory();
		System.out.println( String.format("number of processor is : %d", numProcessor) );
		System.out.println( String.format("JVM free memory size is : %d", freeMemory) );
		System.out.println( String.format("maxMemory : %d", maxMemory) );
		System.out.println( String.format("totalMemory : %d", totalMemory) );
	}
	
	private void fetchSysEnv(){
		Map<String,String> map = System.getenv();
		Set<String> keysets = map.keySet();
		for(String key : keysets){
			System.out.println( String.format("%s : %s", key, map.get(key)) );
		}
	}
	
	private void fetchSysProp(){
		System.getProperties().list(System.out);
	}
}
